<?php

declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Controller\Commerce_order;

use Plugin\Attractions\Commerce\Service\Commerce_order\CommerceOrderService;
use Plugin\Attractions\Commerce\Request\Commerce_order\CommerceOrderRequest;
use Plugin\Attractions\Commerce\Schema\Commerce_order\CommerceOrderSchema;
use App\Http\Common\Result;
use Hyperf\HttpServer\Request;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Swagger\Annotation\Delete;
use Hyperf\Swagger\Annotation\Get;
use Hyperf\Swagger\Annotation\HyperfServer;
use Hyperf\Swagger\Annotation\JsonContent;
use Hyperf\Swagger\Annotation\Post;
use Hyperf\Swagger\Annotation\Put;
use Hyperf\Swagger\Annotation\RequestBody;
use Mine\Access\Attribute\Permission;
use Mine\Swagger\Attributes\PageResponse;
use App\Http\Admin\Controller\AbstractController as AdminAbstractController;
use Hyperf\Context\Context;
use Plugin\Attractions\AttractionManagement\Middleware\CheckJwtTokenMiddleware;
use Plugin\Attractions\Commerce\Request\Commerce_order\CommercePlaceOrderRequest;

/**
 * 订单控制器
 * Class CommerceOrderController
 */
#[HyperfServer(name: 'http')]
#[Middleware(CheckJwtTokenMiddleware::class, priority: 100)]
final class CommerceOrderApiController extends AdminAbstractController
{

    /**
     * 业务处理服务
     * CommerceOrderService
     */
    public function __construct(
        protected readonly CommerceOrderService $service
    ) {}

    #[Get(
        path: '/commerce_order/commerceOrder/api/list',
        summary: '订单控制器列表',
        tags: ['订单控制器'],
    )]
    #[PageResponse(instance: CommerceOrderSchema::class)]
    public function page(Request $request): Result
    {
        return $this->success($this->service->apiPage(array_merge([
            'customer_id' => Context::get('jwtData')['id'],
        ], $request->all()), (int) $request->query('page'), (int) $request->query('page_size')));
    }

    #[Put(
        path: '/commerce_order/commerceOrder/api/cancel/{id}',
        operationId: 'Commerce_orderCommerceorderEdit',
        summary: '取消订单控制器',
        tags: ['订单控制器']
    )]
    #[PageResponse(instance: new Result())]
    public function cancel(int $id, Request $request): Result
    {
        $this->service->cancelOrder($id, array_merge([
            'customer_id' => Context::get('jwtData')['id'],
        ], $request->all()));
        return $this->success();
    }

    #[Delete(
        path: '/commerce_order/commerceOrder/api',
        operationId: 'Commerce_orderCommerceorderDelete',
        summary: '删除订单控制器',
        tags: ['订单控制器']
    )]
    #[PageResponse(instance: new Result())]
    public function delete(): Result
    {
        $this->service->deleteOrder(array_merge([
            'customer_id' => Context::get('jwtData')['id'],
        ], $this->getRequest()->all()));
        return $this->success();
    }

    #[Post(
        path: '/commerce_order/commerceOrder/api/placeOrder',
        operationId: 'Commerce_orderCommerceorderPlaceOrder',
        summary: '下单',
        tags: ['下单控制器'],
    )]
    #[RequestBody(
        content: new JsonContent(ref: CommerceOrderRequest::class, title: '下单控制器')
    )]
    #[PageResponse(instance: new Result())]
    public function placeOrder(CommercePlaceOrderRequest $request): Result
    {
        $data = $this->service->placeOrder(intval(Context::get('jwtData')['id']), $request->post());
        return $this->success($data);
    }
}
